﻿<?php
class qqzan{
	function set($qq,$sid,$skey=0){
		$this->qq=$qq;
		$this->sid=$sid;
		$this->skey=$skey;
		$this->gtk=$this->getGTK($skey);
		$this->cookie='uin=o0'.$qq.'; skey='.$skey.';';
	}
	function getnew($do=0){
		if($this->sid){		
            if(!isset($stop)){
            $url="http://m.qzone.com/get_feeds?g_tk=".time()."&res_type=0&refresh_type=2&format=json&sid=".$this->sid;
			$json=$this->getcurl($url);
			$arr=json_decode($json,true);
			if(@array_key_exists('code',$arr) && $arr['code']==0){
				$shuos=$arr['data']['vFeeds'];
				foreach($shuos as $shuo){
					$like=$shuo['like']['isliked'];
					if($like==0){
						$appid=$shuo['comm']['appid'];
						$typeid=$shuo['comm']['feedstype'];
						$curkey=$shuo['comm']['curlikekey'];
						$uinkey=$shuo['comm']['orglikekey'];
						$uin=$shuo['userinfo']['user']['uin'];
						$from=$shuo['userinfo']['user']['from'];
						$abstime=$shuo['comm']['time'];
						$cellid=$shuo['id']['cellid'];
						if($do){
							$this->pclike($curkey,$uinkey,$from,$appid,$typeid,$abstime,$cellid);
						}else{
							$this->sendlike($uin,$appid,$uinkey,$curkey);
						}
						sleep(1);
					}
				}
			}elseif($arr[message]=='请先登录'){
				$this->sidzt=1;
			}
            }
		}
	}
	function pclike($curkey,$uinkey,$from,$appid,$typeid,$abstime,$fid){
		$post="qzreferrer=http://user.qzone.qq.com/".$this->qq."&opuin=".$this->qq."&unikey={$uinkey}&curkey={$curkey}&from={$from}&appid={$appid}&typeid={$typeid}&abstime={$abstime}&fid={$fid}&active=0&fupdate=1";
		$url="http://w.qzone.qq.com/cgi-bin/likes/internal_dolike_app?g_tk=".$this->gtk;
		$get=$this->getcurl($url,$post,$this->cookie);
		preg_match('/callback\((.*?)\)\;/is',$get,$json);
		if($json=$json[1]){
			$arr=json_decode($json,true);
			if($arr[code]==-3000){
				$this->skeyzt=1;
			}
		}else{
			$this->error[]='获取赞结果失败[PC]';
		}
		
	}
	function sendlike($uin,$type,$uinkey,$curkey){
		$post="opr_type=like&action=0&res_uin=$uin&res_type=$type&uin_key=$uinkey&cur_key=$curkey&format=json&sid=".$this->sid;
		$url="http://m.qzone.com/praise/like?g_tk=".$this->gtk;
		$json=$this->getcurl($url,$post);
		$arr=json_decode($json,true);		
	}
	function getcurl($url,$post=0,$cookie=0){
		$ch=curl_init($url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		if($post){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		}
		if($cookie){
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		}
		$content = curl_exec($ch);
		curl_close($ch); 
		return $content;
	} 
	private function getGTK($skey){
        $len = strlen($skey);
        $hash = 5381;
        for($i = 0; $i < $len; $i++){
            $hash += ($hash << 5) + ord($skey[$i]);
        }
        return $hash & 0x7fffffff;//计算g_tk
    }
}
